package code.lucamarrocco.rest;

import java.util.*;

public class MethodsOrGoals {

	public static final String goalOf(String queryString) {
		return methodOf(queryString);
	}

	public static final List<RestfulResource> goalsOf(RestfulProtocol protocol) {
		return protocol.getResources();
	}

	public static final List<RestfulResource> goalsOf(RestfulTopic topic) {
		return topic.getResources();
	}

	public static final String methodOf(String queryString) {
		if (queryString == null) return "";
		if (queryString.matches("^[^=&]*$")) return "!" + queryString;
		if (queryString.matches("^[^=&]*&*$")) return "!" + queryString.replaceAll("&", "");
		if (queryString.matches("^&*[^=&]*&*$")) return "!" + queryString.replaceAll("&", "");
		return "";
	}

	public static final List<String> methodsOf(RestfulProtocol protocol) {
		List<String> strings = new LinkedList<String>();
		for (RestfulMethod method : protocol.getMethods()) {
			strings.add(method.toString());
		}
		return strings;
	}

	public static final List<RestfulMethod> methodsOf(RestfulProtocol protocol, String goal) {
		for (RestfulResource resource : protocol.getResources()) {
			if (resource.getName().equals(goal)) return resource.getMethods();
		}
		return null;
	}

	public static final List<RestfulParameter> requiredParametersOf(RestfulProtocol protocol, String goal) {
		List<RestfulParameter> parameters = new LinkedList<RestfulParameter>();

		for (RestfulResource resource : protocol.getResources()) {
			if (resource.getName().equals(goal)) for (RestfulMethod method : resource.getMethods())
				parameters.addAll(method.getRequiredParameters());
		}
		
		return parameters;
	}

	public static final List<String> examplesOf(RestfulProtocol protocol, String goal) {
		List<String> examples = new LinkedList<String>();

		for (RestfulResource resource : protocol.getResources()) {
			if (resource.getName().equals(goal)) for (RestfulMethod method : resource.getMethods())
				examples.addAll(method.getExamples());
		}
		
		return examples;
	}

	public static final List<RestfulParameter> optionalParametersOf(RestfulProtocol protocol, String goal) {
		List<RestfulParameter> parameters = new LinkedList<RestfulParameter>();

		for (RestfulResource resource : protocol.getResources()) {
			if (resource.getName().equals(goal)) for (RestfulMethod method : resource.getMethods())
				parameters.addAll(method.getOptionalParameters());
		}
		
		return parameters;
	}

	public static final <T> List<String> names(List<T> list) {
		List<String> names = new LinkedList<String>();

		if (list.isEmpty()) return names;

		T first = list.iterator().next();

		if (first instanceof RestfulResource) {
			for (T next : list) {
				names.add(((RestfulResource) next).getName());
			}
		}

		if (first instanceof RestfulTopic) {
			for (T next : list) {
				names.add(((RestfulTopic) next).getName());
			}
		}

		return names;
	}

	public static final <T> List<String> patterns(List<RestfulMethod> methods) {
		List<String> names = new LinkedList<String>();
		for (RestfulMethod method : methods) {
			names.add(method.getPattern());
		}
		return names;
	}

	public static final List<RestfulResource> resourcesOf(RestfulProtocol protocol) {
		return goalsOf(protocol);
	}

	public static final RestfulTopic topic(RestfulProtocol protocol, String topicName) {
		for (RestfulTopic topic : protocol.getTopics()) {
			if (topic.getName().equals(topicName)) return topic;
		}
		return null;
	}

	public static final List<RestfulTopic> topicsOf(RestfulProtocol protocol) {
		return protocol.getTopics();
	}
}
